package pers.qianyu.diytomcat.util;

import cn.hutool.core.convert.*;
import cn.hutool.core.io.*;
import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
import pers.qianyu.diytomcat.catalina.*;
import pers.qianyu.diytomcat.constants.*;

import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-07 15:44
 */
public class ServerXmlUtil {
    /**
     * 获取所有Context
     *
     * @return
     */
    public static List<Context> getContexts(Host host) {
        ArrayList<Context> contexts = new ArrayList<>();
        String xml = FileUtil.readUtf8String(Locations.SERVER_XML);
        Document document = Jsoup.parse(xml);
        Elements elements = document.select("Context");
        for (Element element : elements) {
            String path = element.attr("path");
            String docBase = element.attr("docBase");
            boolean reloadable = Convert.toBool(element.attr("reloadable"), true);
            Context context = new Context(path, docBase, host, reloadable);
            contexts.add(context);
        }
        return contexts;
    }

    /**
     * 获取所有Host
     *
     * @param engine
     * @return
     */
    public static List<Host> getHosts(Engine engine) {
        ArrayList<Host> hosts = new ArrayList<>();
        String xml = FileUtil.readUtf8String(Locations.SERVER_XML);
        Document document = Jsoup.parse(xml);
        Elements elements = document.select("Host");
        for (Element element : elements) {
            String name = element.attr("name");
            hosts.add(new Host(name, engine));
        }
        return hosts;
    }

    public static String getEngineDefaultHost() {
        String xml = FileUtil.readUtf8String(Locations.SERVER_XML);
        Document document = Jsoup.parse(xml);
        Element engine = document.select("Engine").first();
        return engine.attr("defaultHost");
    }

    public static String getServiceName() {
        String xml = FileUtil.readUtf8String(Locations.SERVER_XML);
        Document document = Jsoup.parse(xml);
        Element service = document.select("Service").first();
        return service.attr("name");
    }

    public static List<Connector> getConnectors(Service s) {
        String xml = FileUtil.readUtf8String(Locations.SERVER_XML);
        Document document = Jsoup.parse(xml);
        Element service = document.select("Service").first();
        Elements elements = service.select("Connector");
        ArrayList<Connector> connectors = new ArrayList<>();
        for (Element element : elements) {
            int port = Integer.parseInt(element.attr("port"));
            String compression = element.attr("compression");
            int compressionMinSize = Convert.toInt(element.attr("compressionMinSize"), 0);
            String noCompressionUserAgents = element.attr("noCompressionUserAgents");
            String compressableMimeType = element.attr("compressableMimeType");
            // 设置属性
            Connector connector = new Connector(s);
            connector.setCompressableMimeType(compressableMimeType);
            connector.setCompression(compression);
            connector.setCompressionMinSize(compressionMinSize);
            connector.setNoCompressionUserAgents(noCompressionUserAgents);
            connector.setPort(port);
            connectors.add(connector);
        }
        return connectors;
    }
}
